﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Linq;

namespace Biljart.Model.Dao
{
    sealed public class UserDao : IUserDao
    {
        private readonly BiljartDataClassesDataContext _context;

        public UserDao()
        {
            _context = new BiljartDataClassesDataContext();
        }

        public User GetUser(long id)
        {
            var user = (from u in _context.Users
                        where u.Id == id
                        select u).FirstOrDefault();
            
            return user;
        }

        public User GetUser(string nickname)
        {
            var user = (from u in _context.Users
                        where u.Nickname == nickname
                        select u).FirstOrDefault();
            return user;
        }

       

        public IEnumerable<User> GetUsers()
        {
            return _context.Users;
        }

        public IEnumerable<User> GetUsers(HashSet<long> ids )
        {
            var users = (from u in _context.Users
                         where ids.Contains(u.Id)
                         select u).ToList();
            return users;
        }

        public bool AddUser(User user)
        {
            try
            {
                _context.Users.InsertOnSubmit(user);
                _context.SubmitChanges();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
            
        }

        public bool DeleteUser(User user)
        {
            try
            {
                _context.Users.DeleteOnSubmit(user);
                _context.SubmitChanges();
                return true;
            }
            catch (SqlException)
            {
                 return false;
            }
        }
    }
}
